﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using dal;
using System.Text;

/// <summary>
///  股票池业务层
/// </summary>
public class StockPoolBLL
{
    /// <summary>
    /// 机构股票池分页
    /// </summary>
    /// <param name="skip">跳过多少条记录</param>
    /// <param name="top">返回多少条记录</param>
    /// <returns>NewStock_BData_C</returns>
    public NewStock_BData_C 机构股票池(int skip, int top)
    {
        string key = "机构股票池_" + skip.ToString() + "_" + top.ToString();
        var data = (NewStock_BData_C)MemcachedTools.Get(key);
        if (data!=null)
        {
            return data;
        }
        WebsiteDataEntities db = new WebsiteDataEntities();
        var cdata = (from a in db.BData
                    join b in db.Organization on a.Oid equals b.Id
                    where a.InvestmentGrade == "增持" || a.InvestmentGrade == "买入"
                    orderby a.CDate descending
                    select new NewStock_BData { Analyst = a.Analyst, CDate = a.CDate, id = a.id, InvestmentGrade = a.InvestmentGrade, Oid = a.Oid, OName = b.Name, PE1 = a.PE1, PE2 = a.PE2, PerIncome1 = a.PerIncome1, PerIncome2 = a.PerIncome2, StockId = a.StockId, TargetPrice = a.TargetPrice, Title = a.Title });
        int count = cdata.Count();
        List<NewStock_BData> data3= cdata.Skip(skip).Take(top).ToList();
        CommonBLL bll = new CommonBLL();
        foreach (var t in data3)
        {
            t.OName = GetOName(t.Oid);
            try
            {
                t.StockName = bll.DicStock[t.StockId];
            }
            catch
            {
            }
        }
        var c = new NewStock_BData_C() { count = count, list = data3 };
        MemcachedTools.Add(key,c, DateTime.Now.AddHours(1));
        return c;
    }

    /// <summary>
    /// 投资评估调高 分页
    /// </summary>
    /// <param name="skip">跳过多少条</param>
    /// <param name="top">返回多少行</param>
    /// <returns>NewStock_BData_tzpjtg_C</returns>
    public NewStock_BData_tzpjtg_C 投资评估调高(int skip, int top)
    {
        string key = "投资评估调高_" + skip.ToString() + "_" + top.ToString();
        var data = (NewStock_BData_tzpjtg_C)MemcachedTools.Get(key);
        if (data != null)
        {
            return data;
        }
        WebsiteDataEntities db = new WebsiteDataEntities();
        var data1 =from a in db.tzData
                    from b in db.BData
                    where a.bid == b.id
                    orderby b.CDate descending
                    select new NewStock_BData_tzpjtg { Analyst = b.Analyst, CDate = b.CDate, id = b.id, InvestmentGrade = b.InvestmentGrade, Oid = b.Oid, PE1 = b.PE1, PE2 = b.PE2, PerIncome1 = b.PerIncome1, PerIncome2 = b.PerIncome2, StockId = b.StockId, TargetPrice = b.TargetPrice, Title = b.Title, SCPJ=a.spj };
        var count = data1.Count();
        var data2 = data1.Skip(skip).Take(top).ToList();
        CommonBLL bll = new CommonBLL();
        foreach (var c in data2)
        {
            c.OName = GetOName(c.Oid);
            try
            {
                c.StockName = bll.DicStock[c.StockId];
            }
            catch
            {
            }
        }
        var d=  new NewStock_BData_tzpjtg_C() { count = count, list = data2 };
        MemcachedTools.Add(key, d, DateTime.Now.AddHours(1));
        return d;
    }

    /// <summary>
    /// 业绩预测分页
    /// </summary>
    /// <param name="skip">跳过多少行</param>
    /// <param name="top">返回多少行</param>
    /// <returns>NewStock_BData_C</returns>
    public NewStock_BData_C 业绩预测(int skip, int top)
    {
        string key = "业绩预测_" + skip.ToString() + "_" + top.ToString();
        NewStock_BData_C data = (NewStock_BData_C)MemcachedTools.Get(key);
        if (data != null)
        {
            return data;
        }
        WebsiteDataEntities db = new WebsiteDataEntities();
        var data1= db.BData.Select(x => new NewStock_BData { Analyst = x.Analyst, CDate = x.CDate, id = x.id, InvestmentGrade = x.InvestmentGrade, Oid = x.Oid, PE1 = x.PE1, PE2 = x.PE2, PerIncome1 = x.PerIncome1, PerIncome2 =x.PerIncome2, StockId = x.StockId, TargetPrice = x.TargetPrice, Title = x.Title }).OrderByDescending(x=>x.CDate);
        int count = data1.Count();
        var data2 = data1.Skip(skip).Take(top).ToList();
        CommonBLL bll=new CommonBLL();
        foreach (var c in data2)
        {
            c.OName = GetOName(c.Oid);
            try
            {
                c.StockName = bll.DicStock[c.StockId];
            }
            catch
            {
            }
        }
        var data3 = new NewStock_BData_C() { count = count, list = data2 };
        MemcachedTools.Add(key, data3, DateTime.Now.AddDays(1));
        return data3;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="skip"></param>
    /// <param name="top"></param>
    /// <returns></returns>
    public NewStock_BData_C 机构首次关注股票(int skip, int top)
    {
        string key = "机构首次关注股票_" + skip.ToString() + "_" + top.ToString();
        var data = (NewStock_BData_C)MemcachedTools.Get(key);
        if (data != null)
        {
            return data;
        }
        WebsiteDataEntities db = new WebsiteDataEntities();
        var data1 = from a in db.BData
                    group a by new { a.Oid, a.StockId } into g
                    select new{ g.Key.Oid,g.Key.StockId,cdate= g.Min(p=>p.CDate)};
        var data2 = from a in data1
                    from b in db.BData 
                    where a.Oid==b.Oid && a.cdate==b.CDate && a.StockId==b.StockId
                    orderby a.cdate descending
                     select new NewStock_BData { Analyst = b.Analyst, CDate = b.CDate, id = b.id, InvestmentGrade = b.InvestmentGrade, Oid = a.Oid, PE1 = b.PE1, PE2 = b.PE2, PerIncome1 = b.PerIncome1, PerIncome2 = b.PerIncome2, StockId = a.StockId, TargetPrice = b.TargetPrice, Title = b.Title };
        int count=data2.Count();
        var data3=data2.Skip(skip).Take(top).ToList();
        CommonBLL bll = new CommonBLL();
        foreach (var c in data3)
        {
            c.OName = GetOName(c.Oid);
            try
            {
                c.StockName = bll.DicStock[c.StockId];
            }
            catch
            {
            }
        }
        NewStock_BData_C data4=new NewStock_BData_C(){ list=data3, count=count};
        MemcachedTools.Add(key, data4, DateTime.Now.AddHours(1));
        return data4;
    }

    public NewStock_BData_jggzdzg_C 机构关注度最高(int skip, int top)
    {
        string key = "机构关注度最高_" + skip.ToString() + "_" + top.ToString();
        var data = (NewStock_BData_jggzdzg_C)MemcachedTools.Get(key);
        if (data != null)
        {
            return data;
        }
         
        WebsiteDataEntities db = new WebsiteDataEntities();
        DateTime updateTime=DateTime.Now.AddDays(-1000);
        var cdata = db.Stock.Where(x => x.Attention > 0 && x.UpdateDate > updateTime).OrderByDescending(x=>x.Attention).Select(x => new NewStock_BData_jggzdzg { Attention = x.Attention , InvestmentGrade = x.InvestmentGrade, Name = x.Name, PE1 = x.PE1, PE2 = x.PE2, PerIncome1 = x.PerIncome1, PerIncome2 = x.PerIncome2, Stock = x.Id, TargetPrice = x.TargetPrice });
        int count = cdata.Count();
        var data2 = cdata.Skip(skip).Take(top).ToList();
        var data3 = new NewStock_BData_jggzdzg_C() { count = count, list = data2 };
        MemcachedTools.Add(key, data3, DateTime.Now.AddHours(1));
        return data3;
    }

    public NewStock_BData_C 盈利预测调高(int skip, int top)
    {
        string key = "盈利预测调高_" + skip.ToString() + "_" + top.ToString();
        var data3 = (NewStock_BData_C)MemcachedTools.Get(key);
        if (data3 != null)
        {
            return data3;
        }
        WebsiteDataEntities db = new WebsiteDataEntities();
        DateTime updateTime = DateTime.Now.AddDays(-1000);
        var data1 = from a in db.BData
                    join b in db.Organization on a.Oid equals b.Id
                    where a.InvestmentGrade=="买入" || a.InvestmentGrade=="增持" && a.CDate>updateTime
                    orderby a.CDate descending
                    select new NewStock_BData { Analyst = a.Analyst, CDate = a.CDate, id = a.id, InvestmentGrade = a.InvestmentGrade, Oid = a.Oid, OName = b.Name, PE1 = a.PE1, PE2 = a.PE2, PerIncome1 = a.PerIncome1, PerIncome2 = a.PerIncome2, StockId = a.StockId, TargetPrice = a.TargetPrice, Title = a.Title };
        var data2 = data1.Skip(skip).Take(top).ToArray();
        CommonBLL bll = new CommonBLL();
        foreach (var c in data2)
        {
            c.OName = GetOName(c.Oid);
            try
            {
                c.StockName = bll.DicStock[c.StockId];
            }
            catch
            {
            }
        }
        data3 =new NewStock_BData_C(){ count=data1.Count(),list=data2.ToList()};

        MemcachedTools.Add(key, data3, DateTime.Now.AddHours(1));

        return data3;
    }


    public string GetOName(string id)
    {
        if (GetOrganizations.Any(x => x.Id == id))
        {
            return GetOrganizations.First(x => x.Id == id).Name;
        }
        else
        {
            return string.Empty;
        }
    }

    public List<Organization> GetOrganizations
    {
        get
        {
            var c = (List<Organization>)MemcachedTools.Get("Organizations");
            if (c != null)
            {
                return c;
            }
            else
            {    
                WebsiteDataEntities db = new WebsiteDataEntities();
                c = db.Organization.ToList();
                MemcachedTools.Add("Organizations", c, DateTime.Now.AddHours(1));
                return c;
            }
        }
    }

    public List<NewStock_BData> 股票研报(string stock,int skip,int top)
    {
        string key="NewStock_BData_GetYbByStock_" + stock + skip.ToString() + "_" + top.ToString();
        var data = (List<NewStock_BData>)MemcachedTools.Get("NewStock_BData_GetYbByStock_" + stock + skip.ToString() + "_" + top.ToString());
        if (data != null)
        {
            return data;
        }
        WebsiteDataEntities db = new WebsiteDataEntities();
        var  data1 = from a in db.BData
                   join b in db.Organization on a.Oid equals b.Id
                   where a.StockId==stock
                   orderby a.CDate descending
                   select new NewStock_BData { Analyst = a.Analyst, CDate = a.CDate, id = a.id, InvestmentGrade = a.InvestmentGrade, Oid = a.Oid, OName = b.Name, PE1 = a.PE1, PE2 = a.PE2, PerIncome1 = a.PerIncome1, PerIncome2 = a.PerIncome2, StockId = a.StockId, TargetPrice = a.TargetPrice, Title = a.Title };
        string str = data1.ToString();
        var data2 = data1.Skip(skip).Take(top).ToList();
        MemcachedTools.Add(key, data2, DateTime.Now.AddHours(1));
        return data2;
    }
}